<template>
  <section>
    <mavon-editor v-loading=loading v-model="value" ref="md" @imgAdd="imgAdd" @save="save" @imgDel="imgDel"
                  :ishljs="true"/>
  </section>
</template>
<script>/* eslint-disable indent,space-before-function-paren */
import {Upload} from '../../util/util'
import {readArticle, updateArticle} from '../../api/api'
import {getImageUrl} from '../../api/base'

export default {
  data() {
    return {
      value: '',
      loading: true
    }
  },
  methods: {
    imgAdd(pos, file) {
      Upload(file).then(x => {
        this.$refs.md.$img2Url(pos, getImageUrl(x))
      })
    },
    imgDel() {

    },
    submit() {
      this.loading = true
      updateArticle({
        article_id: this.$route.query.id,
        content: this.value
      }).then(
        data => {
          this.$message.success('保存成功')
          this.loading = false
        }
      ).catch(
        msg => {
          this.loading = false
        }
      )
    },
    save() {
      this.submit()
    }

  },
  mounted() {
    readArticle({article_id: this.$route.query.id}).then(
      data => {
        console.log(data)
        this.value = data[0].content
        this.loading = false
      }
    ).catch()
  }
}
</script>
<style>

</style>
